package com.File_io_.io_.OutputStream;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * @author 宋鑫宇
 * @version 1.0
 * 文件拷贝
 */
public class FileCopy {
    public static void main(String[] args) {
        //1.将文件读入程序，创建输入流
        //2.将文件写入到指定位置，创建输出流
        String srcPath = "C:\\Users\\Administrator\\Pictures\\1.jpg";
        String decPath = "D:\\idea_WorkSpace\\myjava\\myjava\\src\\com\\File_io_\\date\\123.jpg";
        FileInputStream fileInputStream = null;
        FileOutputStream fileOutputStream = null;
        try {
            fileInputStream = new FileInputStream(srcPath);
            fileOutputStream = new FileOutputStream(decPath);
            //定义一个字节数组，提高读取效率
            byte[] buf = new byte[1024];
            int readLen = 0;
            while ((readLen = fileInputStream.read(buf)) != -1){
                //读取到后就写入到目标文件，通过FileOutputStream
                // 即，一边读，一边写
                fileOutputStream.write(buf,0,readLen);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                if (fileInputStream != null){
                    fileInputStream.close();
                }
                if (fileOutputStream != null){
                    fileOutputStream.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
